package com.example.comm.designModel.singleModel;

/**
 * 线程安全的单例模式
 * 通过双重判断来保证单例设计模式在多线程中的安全性，
 * 并且它在性能方面提高了很多。
 */
public class ThreadSafetySingleCase {

    private ThreadSafetySingleCase(){

    }

    private static ThreadSafetySingleCase threadSafetySingleCase = null;

    /**
     * synchronized在方法上加锁 (同步锁)
     * synchronized在代码块内部加锁 （同步代码块）
     * synchronized(同步锁)
     */
    public static ThreadSafetySingleCase getInstance(){
        if (threadSafetySingleCase == null){
            synchronized (threadSafetySingleCase){
                if (threadSafetySingleCase == null){
                    threadSafetySingleCase = new ThreadSafetySingleCase();
                }
            }
        }
        return threadSafetySingleCase;
    }
}
